package leetcode_801_900;

import java.util.Arrays;

public class LeeCode_875 {
    public static void main(String[] args) {
        System.out.println(minEatingSpeed(new int[]{3, 6, 7, 11}, 8));
        System.out.println(minEatingSpeed(new int[]{30,11,23,4,20}, 5));
    }
    private static int minEatingSpeed(int[] piles, int h) {
        int n = piles.length;
        Arrays.sort(piles);
        int l = 1, r = piles[n - 1];
        while (l < r){
            int m = l + (r - l) / 2;
            int count = 0;
            for (int pile : piles) {
                if (pile % m == 0) {
                    count += pile / m;
                } else {
                    count += pile / m + 1;
                }
            }
            if (count > h){
                l = m + 1;
            }else {
                r = m;
            }
        }
        return l;
    }
}
